package com.xuecheng.content.config;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author Sober_wxy
 * @date 2022/7/17 17:42
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class testUpload {
        @Autowired
        private MinioClient minioClient;
        @Autowired
        private MinioProperties minioProperties;
        @Test
        public void testUpload() throws Exception {
                // 指定文件
                FileInputStream inputStream = new FileInputStream("D:\\1.jpg");
                String fileName = "1.jpg";
                // 指定文件上传的 路径 名称
                String filePath = "/images/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date())
                        + UUID.randomUUID() + fileName;
                //构造PutArgs函数 发送请求
                PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                        .contentType("image/jpg")
                        .stream(inputStream, inputStream.available(), -1)
                        .bucket(minioProperties.getBucket())
                        .object(filePath)
                        .build();
                // 发送请求
                minioClient.putObject(putObjectArgs);
                //拼接访问地址
                String urlPath = minioProperties.getDomain() + "/" +minioProperties.getBucket() + filePath;
                System.out.println(urlPath);
        }
}
